package com.yhsoft.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yhsoft.common.response.PageResult;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.mall.controller.request.*;
import com.yhsoft.mall.controller.response.TopicInfoOutVo;
import com.yhsoft.mall.controller.response.TopicListOutVo;
import com.yhsoft.mall.service.ITopicInfoService;
import com.yhsoft.sys.service.IBaicInterfaceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 商城专题信息表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-12
 */
@Api(tags = {"E-小程序内容配置-商品专题"})
@RestController
@RequestMapping("/mall/topicInfo")
public class TopicInfoController {

    @Resource
    private ITopicInfoService iTopicInfoService;

    @Resource
    private IBaicInterfaceConfigService iBaicInterfaceConfigService;

    @ApiOperation("获得专题列表")
    @GetMapping("/getTopicList")
    public Result<PageResult<TopicListOutVo>> getTopicList(@Valid TopicListInVo inVo){
        IPage<TopicListOutVo> iPage = iTopicInfoService.getTopicList(inVo);
        return Result.success(PageResult.valueOf(iPage));
    }
    @ApiOperation("新增专题")
    @PostMapping("/addTopicInfo")
    public Result<?> addTopicInfo(@Valid @RequestBody TopicInfoAddInVo inVo){
        // 先录入appid才能操作
        if(!iBaicInterfaceConfigService.exists(TokenService.getUserBytoken().getCompanyId())){
            return Result.fail("新增专题失败，需要先录入小程序信息！");
        }
        iTopicInfoService.addTopicInfo(inVo);
        return Result.success("新增专题成功");
    }

    @ApiOperation("删除专题")
    @DeleteMapping("/deleteTopicInfo")
    public Result<?> deleteTopicInfo(@NotNull(message = "专题id不能为空") @RequestParam Integer id) {
        iTopicInfoService.deleteTopicInfo(id);
        return Result.success("删除成功！");
    }

    @ApiOperation("设置通用专题模块内容")
    @PostMapping("/setTopicGeneralBar")
    public Result<?> setTopicGeneralBar(@Valid @RequestBody TopicGeneralInVo inVo){
        // 先录入appid才能操作
        if(!iBaicInterfaceConfigService.exists(TokenService.getUserBytoken().getCompanyId())){
            return Result.fail("设置失败，需要先录入小程序信息！");
        }

        iTopicInfoService.setTopicGeneralBar(inVo);
        return Result.success("设置通用专题模块成功");
    }

    @ApiOperation("获得通用专题模块内容")
    @GetMapping("/getTopicGeneralInfo")
    public Result<TopicGeneralInVo> getTopicGeneralInfo(@RequestParam(value = "id", required = true) Integer id){
        TopicGeneralInVo vo =  iTopicInfoService.getTopicGeneralInfo(id);
        return Result.success(vo);
    }

    @ApiOperation("设置充值中心模块内容")
    @PostMapping("/setTopUpBar")
    public Result<?> setTopUpBar(@Valid @RequestBody TopUpBarInVo inVo){
        // 先录入appid才能操作
        if(!iBaicInterfaceConfigService.exists(TokenService.getUserBytoken().getCompanyId())){
            return Result.fail("设置失败，需要先录入小程序信息！");
        }

        if(inVo.getModuleId() == null){
            // 检查是否存在
            if(iTopicInfoService.exists("topUpBar", inVo.getId())){
                return Result.fail("不能重复新增！");
            }

            iTopicInfoService.addTopUpBar(inVo);
            return Result.success("新增充值中心模块成功");
        }else{
            iTopicInfoService.updateTopUpBar(inVo);
            return Result.success("修改充值中心模块成功");
        }
    }

    @ApiOperation("设置品牌会员模块内容")
    @PostMapping("/setBrandBar")
    public Result<?> setBrandBar(@Valid @RequestBody BrandBarInVo inVo){
        // 先录入appid才能操作
        if(!iBaicInterfaceConfigService.exists(TokenService.getUserBytoken().getCompanyId())){
            return Result.fail("设置失败，需要先录入小程序信息！");
        }

        if(inVo.getModuleId() == null){
            // 检查是否存在
            if(iTopicInfoService.exists("brandBar", inVo.getId())){
                return Result.fail("不能重复新增！");
            }

            iTopicInfoService.addBrandBar(inVo);
            return Result.success("新增品牌会员模块成功");
        }else{
            iTopicInfoService.updateBrandBar(inVo);
            return Result.success("修改品牌会员模块成功");
        }
    }

    @ApiOperation("设置商品模块内容")
    @PostMapping("/setTopicGoodsBar")
    public Result<?> setTopicGoodsBar(@Valid @RequestBody TopicGoodsBarInVo inVo){
        // 先录入appid才能操作
        if(!iBaicInterfaceConfigService.exists(TokenService.getUserBytoken().getCompanyId())){
            return Result.fail("设置失败，需要先录入小程序信息！");
        }

        if(inVo.getModuleId() == null){

            // 检查是否存在
            if(iTopicInfoService.exists("goodsBar", inVo.getId())){
                return Result.fail("不能重复新增！");
            }

            iTopicInfoService.addGoodsBar(inVo);
            return Result.success("新增商品模块成功");
        }else{
            iTopicInfoService.updateGoodsBar(inVo);
            return Result.success("修改商品模块成功");
        }
    }

    @ApiOperation("获得充值专题配置")
    @GetMapping("/getTopUpTopicInfo")
    public Result<TopicInfoOutVo> getTopUpTopicInfo(@RequestParam(value = "id", required = true) Integer id){
        TopicInfoOutVo outVo =  iTopicInfoService.getTopUpTopicInfo(id);
        return Result.success(outVo);
    }

}
